package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.wenli.graduate2022.wenliGreenShop.entity.ProductOrderItem;
import com.wenli.graduate2022.wenliGreenShop.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HuangHai
 * @date 2022/5/2 18:25
 */
@Repository
public interface ShoppingCartMapper {
    @Results(id = "shoppingCart",value= {
            @Result(property = "shoppingcartProduct", column = "shoppingcart_product_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.ProductMapper.findById",
                            fetchType = FetchType.LAZY)),
            @Result(property = "shoppingcartUser", column = "shoppingcart_user_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.UserMapper.findById",
                            fetchType = FetchType.LAZY))
    })
    @Select("select * from shoppingCart where shoppingcart_product_id = #{productId}")
    List<ShoppingCart> findByProductId(Integer productId);    //根据产品id查询所有，可以用于查询该产品已经销售了多少件


    @Select("select * from shoppingCart where shoppingcart_id = #{id}")
    @ResultMap("shoppingCart")
    ShoppingCart findById(Integer id);        //根据id查询购物车


    @Select("select * from shoppingCart where shoppingcart_user_id = #{id}")
    @ResultMap("shoppingCart")
    List<ShoppingCart> findByUserId(Integer id);  //根据用户id查询该用户下的所有产品订单

    @Insert("insert into shoppingCart(shoppingcart_number,shoppingcart_price," +
            "shoppingcart_product_id,shoppingcart_user_id) values(#{ShoppingcartNumber},#{ShoppingcartPrice}," +
            "#{ShoppingcartProduct.productId},#{ShoppingcartUser.userId})")
    @ResultMap("shoppingCart")
    int addShoppingCart(ShoppingCart shoppingCart);


    @Update("update shoppingCart set shoppingcart_number=#{ShoppingcartNumber},shoppingcart_price=#{ShoppingcartPrice}," +
            "shoppingcart_product_id=#{ShoppingcartProduct.productId}" +
            " where shoppingcart_user_id =#{ShoppingcartUser.userId} and shoppingcart_product_id=#{ShoppingcartProduct.productId} ")
    @ResultMap("shoppingCart")
    int updateShoppingCart(ShoppingCart shoppingCart);

    @Delete("delete from shoppingCart where shoppingcart_id = #{shoppingCartId}")
    @ResultMap("shoppingCart")
    int deleteShoppingCart(Integer shoppingCartId);

}
